﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NHibernate;

namespace PhotoBook.Repositorio
{
    public class RepositorioAlbum : RepositorioBase , PhotoBook.Model.NHibernate.IRepositorioAlbum 
    {
        #region IGenericRepository<Album> Members

        public int Count()
        {
            throw new NotImplementedException();
        }

        public void Delete(PhotoBook.Model.Entities.Album obj)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                session.Delete(obj);
            }  
        }

        public int Insert(PhotoBook.Model.Entities.Album obj)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                using(session.BeginTransaction())
                {
                    session.Save(obj);
                    session.Transaction.Commit();
                    return obj.IdAlbum;
                }
            }  
        }

        public IList<PhotoBook.Model.Entities.Album> Select()
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                ICriteria criteria = session.CreateCriteria(typeof(PhotoBook.Model.Entities.Album));
                return criteria.List<PhotoBook.Model.Entities.Album>();
            } 
        }

        public PhotoBook.Model.Entities.Album SelectById(object id)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                return session.Get<PhotoBook.Model.Entities.Album>(id);
            }
        }

        public void Update(PhotoBook.Model.Entities.Album obj)
        {
            using (ISession session = this.SessionNHiBernate.OpenSession())
            {
                session.Update(obj);
            } 
        }

        #endregion
    }
}
